//
// Created by ta&to on 2022/8/25.
//

#ifndef TPSERVER_TP_TIME_H
#define TPSERVER_TP_TIME_H
#include <time.h>
#include <sys/time.h>
#include <lock.h>
#include "http_string.h"
#include "http_atomic.h"

#define NGX_TIME_SLOTS   64


static int       slot;

static http_atomic_t      http_time_lock;

typedef struct tm             http_tm_t;

#define http_tm_sec            tm_sec
#define http_tm_min            tm_min
#define http_tm_hour           tm_hour
#define http_tm_mday           tm_mday
#define http_tm_mon            tm_mon
#define http_tm_year           tm_year
#define http_tm_wday           tm_wday
#define http_tm_isdst          tm_isdst

typedef struct {
    time_t      sec;
    int        msec;
    int       gmtoff;
} http_time_t;

extern volatile char*        http_current_msec;
extern volatile http_time_t  *http_cached_time;
extern volatile http_str_t    http_cached_gmt_time;
extern volatile http_str_t    http_cached_err_log_time;


#define ngx_time()           http_cached_time->sec
#define ngx_timeofday()      (http_time_t *) http_cached_time

#define http_gettimeofday(tp)  (void) gettimeofday(tp, NULL);
#define http_msleep(ms)        (void) usleep(ms * 1000)
#define http_sleep(s)          (void) sleep(s)

long http_times();
void http_time_init(void);
void http_time_update(void);
static char* http_monotonic_time(time_t sec, int msec);
void http_gmtime(time_t t, http_tm_t *tp);
void http_localtime(time_t s, http_tm_t *tm);
long long tm_to_ns(struct timespec tm);
struct timespec ns_to_tm(long long ns);
void http_time_sleep(long ssec);
void http_time_sleepm(long msec);
void http_time_sleepn(long nsec);
#endif //TPSERVER_TP_TIME_H
